package com.ygu.Enter2.topic2_1第一个公共结点;

public class FindFirstCommonNode {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode p1 = headA;
        ListNode p2 = headB;

        while (p1 != p2){
            if(p1 == null ){
                //p1指针走到了A链表的最后一位 接下来要走B链表
                p1 = headB;
            }else {
                p1 = p1.next;
            }
            if(p2 == null){
                p2 = headA;
            }else {
                p2 = p2.next;
            }
        }
        return p1;
    }

    public static class ListNode {
     int val;
     ListNode next;
     ListNode(int x) {
          val = x;
         next = null;
      }
  }
}
